/*
package com.example.admin.filter;

import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.jwt.JWT;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Locale;

*/
/**
 * @author 王同学
 *//*

@WebFilter("/*")
@Slf4j
@Component
public class AuthFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("拦截了某些东西");
        //业务
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        //查看是否有放行授权  多态写法
        Boolean flag= (Boolean) request.getAttribute("flag");

        if(flag){
            log.info("找到信号量，并且信号量的值是一个true");
            //放行
            filterChain.doFilter(servletRequest,servletResponse);
        }
        String requestURI =request.getRequestURI();
        log.info(requestURI);
        if ("/admin/login".equals(requestURI)){
            //放行
            filterChain.doFilter(servletRequest,servletResponse);
        }
        //放行所有的get请求 get GET Get -> get
        //目的：只是为了让你学会怎么按照请求类型来放行
        // TODO: 2022/11/7  这个就是把所有的大小写转换成小写，放过前面标记的那个请求
        if ("get".equals(request.getMethod().toLowerCase(Locale.ROOT))){
            filterChain.doFilter(servletRequest,servletResponse);
        }
        // todo 取获取post请求过来的数据
        //1.流->接收前端发送过来的流
        //从前端获取输入字节流
        ServletInputStream requestInputStream = request.getInputStream();
        //将字节流转换为字符流,并设置字符编码为utf-8
        InputStreamReader ir = new InputStreamReader(requestInputStream, StandardCharsets.UTF_8);
        //使用字符缓冲流进行读取
        BufferedReader br = new BufferedReader(ir);
        //2.流->String
        //开始拼装json字符串
        String line = null;
        //用sb的原因是因为sb更快
        StringBuilder sb = new StringBuilder();
        while((line = br.readLine())!=null) {
            sb.append(line);
        }
        //3.String(json字符串)->json对象
        //这是比较老的一种转换方法
    //   HashMap<String,String> map= (HashMap<String, String>) JSONObject.parse(sb.toString());
        HashMap map = JSONObject.parseObject(sb.toString(), HashMap.class);
        log.info("{}",map.get("token"));
        //拿到token
        String token = map.get("token").toString();
        //开始验证token
        // 密钥
        byte[] key = "wjx123..".getBytes();
        // 默认验证HS265的算法
        boolean verify = JWT.of(token).setKey(key).verify();

            if (verify){
                filterChain.doFilter(servletRequest,servletResponse);
            }
        log.info("{}",verify);
        log.info("回到这边第一层");
    }
}
*/
